package com.bm.specialvehiclefordispatcher.activity.login;

import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import cn.jpush.android.api.JPushInterface;

import com.android.volley.VolleyError;
import com.bm.corelibs.logic.BaseLogic.NListener;
import com.bm.corelibs.utils.ToastMgr;
import com.bm.specialvehiclefordispatcher.R;
import com.bm.specialvehiclefordispatcher.Tools.Tools;
import com.bm.specialvehiclefordispatcher.activity.BaseActivity;
import com.bm.specialvehiclefordispatcher.activity.main.MainActivity;
import com.bm.specialvehiclefordispatcher.bean.BaseData;
import com.bm.specialvehiclefordispatcher.constants.Constant;
import com.bm.specialvehiclefordispatcher.logics.LoginManager;
import com.bm.specialvehiclefordispatcher.views.CommonNavigationBar;

public class LoginActivity extends BaseActivity implements OnClickListener {
	CommonNavigationBar bar;
	EditText login_tel;
	EditText login_psw;
	Button login_sure;
	Button login_new;
	TextView forget_textview;
	LoginManager manager = new LoginManager();
	boolean fromCreateOrder;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login);
		findViews();
		init();
		addListeners();
	}

	@Override
	public void findViews() {
		bar = (CommonNavigationBar) findViewById(R.id.navi);
		login_tel = (EditText) findViewById(R.id.login_tel);
		login_psw = (EditText) findViewById(R.id.login_psw);
		login_sure = (Button) findViewById(R.id.login_sure);
		login_new = (Button) findViewById(R.id.login_new);
		forget_textview = (TextView) findViewById(R.id.forget_textview);
	}

	@Override
	public void init() {
		bar.setTitle("站点登录");
		fromCreateOrder = getIntent().getBooleanExtra("fromCreateOrder", false);
		bar.setListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				startActivity(new Intent(mActivity, MainActivity.class));
				finish();
			}
		});
	}

	@Override
	public void addListeners() {
		login_sure.setOnClickListener(this);
		login_new.setOnClickListener(this);
		forget_textview.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.login_sure:
			if (Tools.validatePhone(Tools.getTextViewText(login_tel))) {
				if (Tools.getTextViewText(login_psw).length() >= 6) {
					loadingDialog.show();
					manager.login(Tools.getTextViewText(login_tel),
							Tools.getTextViewText(login_psw), loginListener());
				} else {
					showToast("请输入6-16位密码");
				}
			} else {
				showToast("请输入规范的手机号码");
			}
			break;
		case R.id.login_new:
			startActivity(new Intent(LoginActivity.this, RegisterActivity.class));
			break;
		case R.id.forget_textview:
			startActivity(new Intent(LoginActivity.this,
					ForgetPasswordActivity.class));
			break;

		default:
			break;
		}

	}

	private NListener<BaseData> loginListener() {
		return new NListener<BaseData>() {
			@Override
			public void onResponse(BaseData data) {
				loadingDialog.dismiss();
				if (data.status == Constant.STATUS_OK) {
					if (data.data.dispatcher != null)
						LoginManager.getInstance(mActivity).saveUserInfo(
								data.data.dispatcher);
					JPushInterface.setAlias(mActivity,
							data.data.dispatcher.id+"", null);
					if (fromCreateOrder) {
						System.out.println("fromCreateOrder" + fromCreateOrder);
						setResult(RESULT_OK);
						finish();
					} else {
						startActivity(new Intent(LoginActivity.this,
								MainActivity.class));
						finish();
					}
				} else {
					ToastMgr.show(data.msg);
				}
			}

			@Override
			public void onErrResponse(VolleyError error) {
				loadingDialog.dismiss();
			}

			@Override
			public void onAllPageLoaded(int nowPage, int totalPage) {
			}
		};
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			startActivity(new Intent(mActivity, MainActivity.class));
			finish();
		}

		return super.onKeyDown(keyCode, event);
	}
}
